﻿using EM.FrameWork.Paging;
using EM.OA.BLL;
using EM.OA.Common;
using EM.OA.DataModel.DBModel;
using EM.OA.Portal.Models;
using EM.OA.WebCommon.Base;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace EM.OA.Portal.Controllers
{
    public class WorkPlanController : BaseAuthController
    {
        /// <summary>
        /// 返回Index视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewData["refID"] = Guid.NewGuid().ToString();
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        public JsonResult GetList(string page, string rows)
        {
            ViewData["refID"] = Guid.NewGuid().ToString();
            WorkPlanBLL bll = new WorkPlanBLL();
            string planName = GetParam("WorkPlanName");
            string planType = GetParam("WorkPlanType");
            PagedList<EMOA_WorkPlan> list = bll.GetPagedList(Convert.ToInt32(page), Convert.ToInt32(rows),
                p => (planName.IsNullOrEmpty() || p.WorkPlanName == planName) && (planType.IsNullOrEmpty() || p.WorkPlanType == planType), p => p.CreateTime, true);
            return ToJson<EMOA_WorkPlan, WorkPlanModels>(list, list.TotalItemCount);
        }

        /// <summary>
        /// 增加实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(WorkPlanModels model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    WorkPlanBLL bll = new WorkPlanBLL();
                    EMOA_WorkPlan entity = model.ToEntity<EMOA_WorkPlan>();
                    entity.WorkPlanID = Guid.Parse(GetParam("refID"));
                    entity.CreateBy = CurrentUser.UserID;
                    entity.CreateTime = DateTime.Now;
                    if (bll.Add(entity))
                    {
                        return Content("OK");
                    }
                    else
                    {
                        return Content("增加失败");
                    }
                }
                else
                    return Content("验证失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(WorkPlanModels model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    WorkPlanBLL bll = new WorkPlanBLL();
                    EMOA_WorkPlan entity = bll.GetEntity(model.WorkPlanID);
                    entity = model.ToEntity<EMOA_WorkPlan>(entity);
                    entity.UpdateBy = CurrentUser.UserID;
                    entity.UpdateTime = DateTime.Now;
                    if (bll.Update(entity))
                        return Content("OK");
                    else
                        return Content("修改失败");
                }
                else
                {
                    return Content("验证失败");
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string id)
        {
            try
            {
                WorkPlanBLL bll = new WorkPlanBLL();
                if (bll.Delete(Guid.Parse(id)))
                    return Content("OK");
                else
                    return Content("删除失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 获取工作类型树
        /// </summary>
        /// <returns></returns>
        public JsonResult GetWorkPlanTypeTree()
        {
            DictionaryBLL bll = new DictionaryBLL();
            List<EMOA_Dictionary> list = bll.GetDicList(GlobalEnum.RootKind.WorkPlanKind.ToString());
            return ToJson<EMOA_Dictionary, DictionaryModels>(list, list.Count);
        }

        public ActionResult SelectUser()
        {
            //ContentResult content = new ContentResult();
            //content.Content = string.Format("<script language='javaScript' type='text/javaScript'>window.parent.window.location.href = '{0}';window.parent.tb_remove();</script>", "SelectUser");
            //return content;
            return View("SelectUser");
        }
    }
}
